package com.hcl.orm.core.executor.nativesql.tablestruct;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.hcl.orm.core.annotation.DBType;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.security.InvalidParameterException;
import java.util.List;

import static com.hcl.orm.core.executor.DaoMethodExecutor.jdbcTemplateMapperFactory;

/**
 * @author hucl
 * @projectName hcl-orm
 * @description: TODO
 * @date 2021/8/1615:09
 */
@Component
public class OracleTsExecutor implements TsSqlExecutor {
    private static final String COLUMS_SQL_TMPL =
            "select column_name,data_type  from user_tab_columns where  table_name='{}'";
    @Resource
    private JdbcOperations jdbcOperations;


    @Override
    public List<TableColumn> getTableColumns(String tableName) {
        if (StrUtil.isEmpty(tableName)) {
            throw new InvalidParameterException("invalid function params.");
        }

        ResultSetExtractor mapper = jdbcTemplateMapperFactory.newResultSetExtractor(TableColumn.class);
        return (List<TableColumn>) jdbcOperations.query(StrUtil.format(COLUMS_SQL_TMPL, tableName), mapper);
    }

    @Override
    public List<DBType> getTargetDB() {
        return ListUtil.toList(DBType.ORACLE,DBType.OB_ORC);
    }
}
